//
//  YKQHomeViewController.m
//  YaoKongQi
//
//  Created by ryou on 2021/5/19.
//
#define karray @[@"大厅空调",@"卧室空调",@"侧卧空调",@"电视机"]
#import "YKQHomeViewController.h"
#import "YKQAboutViewController.h"
#import "YKQDetailViewController.h"
#import "YKQAddTypeViewController.h"
#import <StoreKit/StoreKit.h>

@interface YKQHomeViewController ()<UITableViewDelegate,UITableViewDataSource,BUNativeExpressRewardedVideoAdDelegate>
@property(nonatomic,strong)UITableView *ykqTableView;
@property(nonatomic,strong)UILabel * timeLabel;
@property(nonatomic,assign)NSInteger ykqloadTag;
@end

@implementation YKQHomeViewController
- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden=YES;
}
-(void)viewDidDisappear:(BOOL)animated{
    self.navigationController.navigationBar.hidden=NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _ykqloadTag = 0;
    [self.view addSubview:  self.ykqTableView];
    
    
    UIView *headView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kYKQWidth, 20 + 74 + 10 + 74)];
    self.ykqTableView.tableHeaderView=headView;
    
    
    UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(20, 20, 300, 74)];
    nameLabel.text =  @"主人，欢迎回家";
    nameLabel.textColor=kBlackColor;
    [headView addSubview:nameLabel];
    nameLabel.font=[UIFont boldSystemFontOfSize:25];
    
    UIButton *addBtn=[[UIButton alloc]initWithFrame:CGRectMake(kYKQWidth - 20 - 74 - 74, 20 , 74, 74)];
    [addBtn setImage:[UIImage imageNamed:@"ykq_btn_add.png"] forState:UIControlStateNormal];
    [headView addSubview:addBtn];
    [addBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *aboutBtn=[[UIButton alloc]initWithFrame:CGRectMake(kYKQWidth - 20 - 74, 20 , 74, 74)];
    [aboutBtn setImage:[UIImage imageNamed:@"ykq_btn_about.png"] forState:UIControlStateNormal];
    [headView addSubview:aboutBtn];
    [aboutBtn addTarget:self action:@selector(aboutBtnClick) forControlEvents:UIControlEventTouchUpInside];

    
    UIImageView *timeImage=[[UIImageView alloc]initWithFrame:CGRectMake(40, addBtn.frame.origin.y+addBtn.frame.size.height+ 10, kYKQWidth - 80, 74)];
    timeImage.image=[UIImage imageNamed:@"ykq_icon_white_bg.png"];
    timeImage.contentMode=UIViewContentModeScaleToFill;
    [headView addSubview:timeImage];
    
    UILabel *timeLabel=[[UILabel alloc]initWithFrame:timeImage.bounds];
    timeLabel.textAlignment=NSTextAlignmentCenter;
    timeLabel.textColor=kRedColor;
    timeLabel.font=[UIFont boldSystemFontOfSize:18];
    [timeImage addSubview:timeLabel];
    _timeLabel =timeLabel;
    
    
    UIButton *doneBtn=[[UIButton alloc]initWithFrame:CGRectMake(100, kYKQHeight/2 - 74/2, kYKQWidth - 200, 74)];
    [doneBtn  setBackgroundImage:[UIImage imageNamed:@"ykq_icon_white_bg.png"] forState:UIControlStateNormal];
    [doneBtn addTarget:self action:@selector(addBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [doneBtn setTitle:@"添加遥控器" forState:UIControlStateNormal];
    [doneBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
    doneBtn.titleLabel.font=[UIFont boldSystemFontOfSize:25];
    [self.ykqTableView addSubview:doneBtn];

    
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(getCurrentTimeAndWeekDay) userInfo:nil repeats:YES];
    [self getCurrentTimeAndWeekDay];
    
    [self ykqLoadRewardVideoAd];
    [self ykqBUAdLoadBanner];
    
    [self performSelector:@selector(ykqPingFen) withObject:nil afterDelay:5.0];
}
-(void)ykqPingFen{
    NSUserDefaults *ykquser=[NSUserDefaults standardUserDefaults];
    BOOL pf =[ykquser boolForKey:@"ykqpingfen"];
    if (!pf) {
        [ykquser setBool:YES forKey:@"ykqpingfen"];
        
        if (@available(iOS 10.3, *)) {
            [SKStoreReviewController requestReview];
        } else {
            // Fallback on earlier versions
        }
    }
}
-(void)aboutBtnClick{
    YKQAboutViewController *about=[[YKQAboutViewController alloc]init];
    [self.navigationController pushViewController:about animated:YES];
}
-(void)addBtnClick{
    [self ykqShowRewardVideoAd];
}
//获取当前时间日期星期
- (void)getCurrentTimeAndWeekDay {
    
    NSArray * arrWeek=@[@"星期天",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
    NSDate *date = [NSDate date];

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [[NSDateComponents alloc] init];

    NSInteger unitFlags = NSCalendarUnitYear |NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitWeekday | NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond;
    comps = [calendar components:unitFlags fromDate:date];
    NSInteger week = [comps weekday];
    NSInteger year=[comps year];
    NSInteger month = [comps month];
    NSInteger day = [comps day];
    NSInteger  hour = [comps hour];
    NSInteger min = [comps minute];
    NSInteger sec = [comps second];
 
    NSString *time =   [NSString stringWithFormat:@"%04ld-%02ld-%02ld  %02ld:%02ld:%02ld  %@",(long)year,(long)month,(long)day,hour,min,sec,[arrWeek objectAtIndex:week - 1]];
    
    _timeLabel.text = time;

    // Do any additional setup after loading the view.
}



-(UITableView *)ykqTableView{
    if (!_ykqTableView) {
        _ykqTableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _ykqTableView.delegate=self;
        _ykqTableView.dataSource=self;
        _ykqTableView.backgroundColor=kWhiteColor;
        _ykqTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    }
    return _ykqTableView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    UITableViewCell *ykqCell=[tableView cellForRowAtIndexPath:indexPath];
    if (!ykqCell) {
        ykqCell=[[UITableViewCell alloc]init];
    }
    ykqCell.selectionStyle=UITableViewCellSelectionStyleNone;
    ykqCell.backgroundColor=kWhiteColor;
    CGFloat itemw= kYKQWidth / 2 - 60;
//    for (int i=0; i<karray.count; i++) {
//        UIImageView *icon=[[UIImageView alloc]initWithFrame:CGRectMake(40 + (itemw + 40) * (i%2), 20+(itemw+40)*(i/2), itemw,itemw)];
//        icon.image=[UIImage imageNamed:@"ykq_icon_home.png"];
//        icon.contentMode=UIViewContentModeScaleAspectFit;
//        [ykqCell.contentView addSubview:icon];
//        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(iconCLick)];
//        [icon addGestureRecognizer:tap];
//        icon.userInteractionEnabled=YES;
//        ykqCell.userInteractionEnabled=YES;
//
//        UILabel *name=[[UILabel alloc]initWithFrame:icon.bounds];
//        name.text = karray[i];
//        name.textAlignment=NSTextAlignmentCenter;
//        name.textColor=kBlackColor;
//        [icon addSubview:name];
//    }
    
    return ykqCell;
}
-(void)iconCLick{
    YKQDetailViewController *detail=[[YKQDetailViewController alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return kYKQHeight;
}

- (void)ykqLoadRewardVideoAd{
 
    BURewardedVideoModel *model = [[BURewardedVideoModel alloc] init];
    self.ykqReWardVideo = [[BUNativeExpressRewardedVideoAd alloc] initWithSlotID:kYkqBuadJili rewardedVideoModel:model];
    self.ykqReWardVideo.delegate = self;
    [self.ykqReWardVideo loadAdData];
    
}

- (void)ykqShowRewardVideoAd {
    if (self.ykqReWardVideo && self.ykqloadTag == 1 ) {
        [[YKQAlertView sharedInstance]showAlertWithWithTag:^{
          
            [self.ykqReWardVideo showAdFromRootViewController:self];
      
        }];

    }else if(self.ykqloadTag == 2){
        [self ykqShowaddViewController];
    }
}
-(void)ykqShowaddViewController{
    self.ykqloadTag = 0 ;
    YKQAddTypeViewController *type=[[YKQAddTypeViewController alloc]init];
    [self.navigationController pushViewController:type animated:YES];
    [self  ykqLoadRewardVideoAd];
}

#pragma mark - BUNativeExpressRewardedVideoAdDelegate

- (void)nativeExpressRewardedVideoAd:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error {
    self.ykqloadTag = 2;
}


- (void)nativeExpressRewardedVideoAdDidDownLoadVideo:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    self.ykqloadTag = 1;
}
 
- (void)nativeExpressRewardedVideoAdDidPlayFinish:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error {
    [self ykqShowaddViewController];
}
- (void)nativeExpressRewardedVideoAdDidClose:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    self.ykqloadTag = 0 ;
    [self  ykqLoadRewardVideoAd];
}
@end
